<template>
  <div class="table-box" style="height: auto">
    <div class="table-search">
      <el-form :inline="true">
        <el-form-item label="充值总计：">
          {{ total.total_amount }} {{ total.currency }}
        </el-form-item>
      </el-form>
    </div>
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-select
            clearable
            v-model="form.merchant_id"
            placeholder="商户"
            filterable
          >
            <el-option
              v-for="item in payMerchantList"
              :key="item.value"
              :label="item.name + '-' + item.merchant_id"
              :value="item.merchant_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item>
          <el-input
            clearable
            placeholder="商户ID"
            v-model="form.merchant_id"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            clearable
            placeholder="商户名称"
            v-model="form.merchant_name"
          ></el-input>
        </el-form-item> -->
        <el-form-item>
          <el-input
            clearable
            placeholder="订单流水号"
            v-model="form.order_no"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="收款人"
            v-model="form.receive_card_name"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="收款账号"
            v-model="form.receive_card_number"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.currency"
            placeholder="币种"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in currencyList"
              :key="item.name"
              :label="item.alias"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.status"
            placeholder="订单状态"
            clearable
            style="width: 120px"
          >
            <el-option label="成功" value="1"></el-option>
            <el-option label="处理中" value="0"></el-option>
            <el-option label="失败" value="2"></el-option>
            <el-option label="取消" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.sys_review_status"
            placeholder="系统审核状态"
            clearable
            style="width: 150px"
          >
            <el-option label="成功" value="1"></el-option>
            <el-option label="审核中" value="0"></el-option>
            <el-option label="失败" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            clearable
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table" style="height: auto">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column
            prop="createtime"
            :formatter="formatterTime"
            label="付款时间"
            align="center"
            width="160"
          />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
            width="120"
          >
            <template #default="scope">
              <el-tooltip
                class="box-item"
                effect="dark"
                :content="scope.row.merchant_name"
                :disabled="!scope.row.merchant_name"
                placement="top"
              >
                <span class="ellipsis">{{
                  scope.row.merchant_name || "--"
                }}</span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="付款金额" align="center">
            <template #default="scope">{{ scope.row.amount }}</template>
          </el-table-column>
          <el-table-column
            label="实际付款金额"
            width="100"
            prop="settled_amount"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="currency"
            label="付款币种"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="fee"
            label="手续费"
            align="center"
          ></el-table-column>
          <el-table-column label="收款姓名" align="center" width="120">
            <template #default="scope">
              <el-tooltip
                class="box-item"
                effect="dark"
                :content="scope.row.receive_card_name"
                :disabled="!scope.row.receive_card_name"
                placement="top"
              >
                <span class="ellipsis">{{
                  scope.row.receive_card_name || "--"
                }}</span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="实际付款日期" align="center" width="160">
            <template #default="scope">
              <div v-if="scope.row.sends.length">
                <div v-for="x in scope.row.sends">
                  {{ formatDates(x.send_pay_time) || "--" }}
                </div>
              </div>
              <p v-else>--</p>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="订单状态" align="center">
            <!--0:处理中;1:成功;2:失败 3:取消-->
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.status == 1
                    ? 'success'
                    : scope.row.status == 0
                    ? 'warning'
                    : 'danger'
                "
              >
                {{
                  scope.row.status == 1
                    ? "成功"
                    : scope.row.status == 2
                    ? "失败"
                    : scope.row.status == 3
                    ? "取消"
                    : "处理中"
                }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="sys_review_status"
            label="系统审核"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.sys_review_status == 1
                    ? 'success'
                    : scope.row.sys_review_status == 0
                    ? 'warning'
                    : 'danger'
                "
              >
                {{
                  scope.row.sys_review_status == 1
                    ? "已审核"
                    : scope.row.sys_review_status == 2
                    ? "拒绝"
                    : "未审核"
                }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="description" label="备注" align="center" />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="210"
          >
            <template #default="scope">
              <el-button
                type="primary"
                size="small"
                @click="handleOperate(scope.$index, scope.row, 'detail')"
                >详情</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 20, 50]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DrawerDetail
      :drawerVisible="drawerVisible"
      @close="close"
      :detailInfo="detailInfo"
    ></DrawerDetail>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import {
  formatterTime,
  pickerDefaultTime,
  formatDates,
  exportOrDownload,
  timestamp,
} from "@/utils/utils";
import { paymentListApi } from "@/api/payServerse/payment";
import { assetsCurrencyApi } from "@/api/payServerse/settings";
import DrawerDetail from "./components/detailDrawer.vue";
import { GlobalStore } from "@/store";

const globalStore = GlobalStore();
const payMerchantList = computed(() => globalStore.payMerchantList);
const loading = ref(false);
const tableData = ref([]);
const currencyList = ref([]);
const detailInfo = ref([]);
const drawerVisible = ref(false);
const formData = {
  merchant_id: "",
  merchant_name: "",
  order_no: "",
  currency: "",
  timeData: [],
};
const form = reactive({ ...formData });
const total = reactive({
  total_amount: 0,
  currency: "",
});
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

//参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    order_type: 2, // 付款
    merchant_id: form.merchant_id,
    merchant_name: form.merchant_name,
    order_no: form.order_no,
    currency: form.currency,
    receive_card_name: form.receive_card_name,
    receive_card_number: form.receive_card_number,
    status: form.status,
    sys_review_status: form.sys_review_status,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};
// 获取币种
const getCurrencyList = async () => {
  try {
    const res = await assetsCurrencyApi({});
    currencyList.value = res.data.results;
  } catch (error) {
    currencyList.value = [];
  }
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await paymentListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    total.currency = res.data.extra.total?.currency;
    total.total_amount = res.data.extra.total?.total_amount;
  } catch (error) {
    loading.value = false;
  }
};
//查询
const onQuery = async () => {
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await paymentListApi(data, "blob");
  exportOrDownload(res, "PayFaz-付款订单列表");
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
// 行操作
const handleOperate = async (index, row, type) => {
  if (type === "detail") {
    drawerVisible.value = true;
    detailInfo.value = row;
  }
};

const close = () => {
  drawerVisible.value = false;
};

onMounted(() => {
  getTbaleData();
  getCurrencyList();
});
</script>
